package com.excesys.exsecs.energy.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ArrayUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.excesys.exsecs.common.core.util.R;
import com.excesys.exsecs.common.excel.annotation.ResponseExcel;
import com.excesys.exsecs.common.log.annotation.SysLog;
import com.excesys.exsecs.energy.entity.DevDeptMap;
import com.excesys.exsecs.energy.service.DevDeptMapService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 部门维度表和设备映射表
 *
 * @author lxc
 * @date 2024-02-26 14:14:13
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/devdeptmap" )
@Tag(description = "bumDevDeptMap" , name = "部门维度表和设备映射表" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class DevDeptMapController {

    private final DevDeptMapService devDeptMapService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param devDeptMap 部门维度表和设备映射表，映射关系如下：
根节点为0，即总体，如山东省设计院或聊城医院，此根节点的映射，在bum_reg_dev中已经完成
每个父节点必须包含子节点下的所有表计，子节点的表计只能在父节点的表计中选取
每个节点的表计必须勾选参与统计部分，方便各维度的统计
设备注册变动时，需要在该表中搜索同步
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('twinops_bumDevDeptMap_view')" )
    public R getDevDeptMapPage(@ParameterObject Page page, @ParameterObject DevDeptMap devDeptMap) {
        LambdaQueryWrapper<DevDeptMap> wrapper = Wrappers.lambdaQuery();
        return R.ok(devDeptMapService.page(page, wrapper));
    }


    /**
     * 通过id查询部门维度表和设备映射表
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询" , description = "通过id查询" )
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('twinops_bumDevDeptMap_view')" )
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(devDeptMapService.getById(id));
    }

    /**
     * 新增部门维度表和设备映射表
     * @param devDeptMap 部门维度表和设备映射表
     * @return R
     */
    @Operation(summary = "新增部门维度表和设备映射表" , description = "新增部门维度表和设备映射表" )
    @SysLog("新增部门维度表和设备映射表" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('twinops_bumDevDeptMap_add')" )
    public R save(@RequestBody DevDeptMap devDeptMap) {
        return R.ok(devDeptMapService.save(devDeptMap));
    }

    /**
     * 修改部门维度表和设备映射表
     * @param devDeptMap 部门维度表和设备映射表
     * @return R
     */
    @Operation(summary = "修改部门维度表和设备映射表" , description = "修改部门维度表和设备映射表" )
    @SysLog("修改部门维度表和设备映射表" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('twinops_bumDevDeptMap_edit')" )
    public R updateById(@RequestBody DevDeptMap devDeptMap) {
        return R.ok(devDeptMapService.updateById(devDeptMap));
    }

    /**
     * 通过id删除部门维度表和设备映射表
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除部门维度表和设备映射表" , description = "通过id删除部门维度表和设备映射表" )
    @SysLog("通过id删除部门维度表和设备映射表" )
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('twinops_bumDevDeptMap_del')" )
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(devDeptMapService.removeBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     * @param devDeptMap 查询条件
   	 * @param ids 导出指定ID
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('twinops_bumDevDeptMap_export')" )
    public List<DevDeptMap> export(DevDeptMap devDeptMap,Long[] ids) {
        return devDeptMapService.list(Wrappers.lambdaQuery(devDeptMap).in(ArrayUtil.isNotEmpty(ids), DevDeptMap::getId, ids));
    }
}